package com.zakler.action;

import java.util.List;

import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.convention.annotation.Results;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;

import com.zakler.po.Teacher;
import com.zakler.util.HibernateUtil;

@Results({ @Result(name = "add_success", location = "teacher_list", type = "redirectAction"),
		@Result(name = "list_success", location = "/addt.jsp", type = ""),
		@Result(name = "searchall_success", location = "/modify.jsp", type = ""),
		@Result(name = "remove_success", location = "teacher_list", type = "redirectAction"),
		@Result(name = "modify_success", location = "teacher_list", type = "redirectAction"),
})
public class TeacherAction extends BaseAction {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private Teacher teacher;
	private Integer tempid;

	// add method
	@Action("teacher_add")
	public String add() throws Exception {
		Session session = HibernateUtil.putSession();
		Transaction transaction = session.beginTransaction();
		session.save(teacher);
		transaction.commit();
		session.close();
		return "add_success";
	}

	// list method
	@Action("teacher_list")
	public String list() throws Exception {

		Session session = HibernateUtil.putSession();
		Query query = session.createQuery("from Teacher");
		@SuppressWarnings("unchecked")
		List<Teacher> list = query.list();
		session.close();
		getHttpServletRequest().getSession().setAttribute("Tlist", list);
		return "list_success";
	}

	// search method
	@Action("teacher_searchAll")
	public String searchAll() throws Exception {

		Session session = HibernateUtil.putSession();
		Query query = session.createQuery("from Teacher");
		@SuppressWarnings("unchecked")
		List<Teacher> list = query.list();
		session.close();
		getHttpServletRequest().getSession().setAttribute("Tlist", list);
		getHttpServletRequest().getSession().setAttribute("TJNum", tempid);
		return "searchall_success";
	}

	// modify method
	@Action("teacher_modify")
	public String modify() throws Exception {

		Session session = HibernateUtil.putSession();
		session.update(teacher);
		Transaction transaction = session.beginTransaction();
		transaction.commit();
		session.close();
		return "modify_success";
	}

	// remove method
@Action("teacher_remove")
	public String remove() throws Exception {
		Session session = HibernateUtil.putSession();
		Query query = session.createQuery("delete from Teacher where id="
				+ tempid.toString());
		query.executeUpdate();
		Transaction transaction = session.beginTransaction();
		transaction.commit();
		session.close();
		return "remove_success";
	}

	public Teacher getTeacher() {

		return teacher;
	}

	public void setTeacher(Teacher teacher) {
		this.teacher = teacher;
	}

	public Integer getTempid() {
		return tempid;
	}

	public void setTempid(Integer tempid) {
		this.tempid = tempid;
	}

}
